﻿Imports System.Data.SqlClient

Public Class clsSubServicios
    Private mintIdSubservicio As Integer
    Private mstrNombre As String
    Private mdblCosto As Double
    Public Property IdSubServicio() As Integer
        Get
            Return mintIdSubservicio
        End Get
        Set(ByVal value As Integer)
            mintIdSubservicio = value
        End Set
    End Property
    Public Property Nombre() As String
        Get
            Return mstrNombre
        End Get
        Set(value As String)
            mstrNombre = value
        End Set
    End Property
    Public Property Costo() As Double
        Get
            Return mdblCosto
        End Get
        Set(ByVal value As Double)
            mdblCosto = value
        End Set
    End Property
    Public Sub New()
        mintIdSubservicio = 0
        mstrNombre = ""
        mdblCosto = 0
    End Sub
    Public Sub Buscar()
        Dim cnConn As New SqlConnection
        cnConn.ConnectionString = CitraConnection

        Dim cmdBuscar As New SqlCommand
        cmdBuscar.Connection = cnConn
        cmdBuscar.CommandText = "SELECT * From SubServicios " & _
        " WHERE Nombre='" & mstrNombre & "'"

        Dim rdBuscar As SqlDataReader

        cnConn.Open()
        rdBuscar = cmdBuscar.ExecuteReader

        If rdBuscar.Read() Then
            mdblCosto = rdBuscar("Costo")
        Else
            mdblCosto = 0
        End If

        rdBuscar.Close()
        cnConn.Close()


    End Sub
    Public Sub Insertar()
        Dim sql As String = "Insert Into SubServicios (" & _
                            "Nombre," & _
                            "Costo) " & _
                            "VALUES (" & _
                            "substring(@Nombre,1,100)," & _
                            "@Costo) "


        Dim cnConn As New SqlConnection
        cnConn.ConnectionString = CitraConnection

        Dim Command As New SqlCommand(sql, cnConn)

        Command.Parameters.AddWithValue("Nombre", mstrNombre)
        Command.Parameters.AddWithValue("Costo", mdblCosto)
        cnConn.Open()
        Dim RowsAffected As Integer = Command.ExecuteNonQuery

        If RowsAffected > 0 Then
            cnConn.Close()
        Else
            MsgBox("No fue posible insertar el subservicio", MsgBoxStyle.Exclamation, "Aviso")
            If cnConn.State = ConnectionState.Open Then
                cnConn.Close()
            End If
        End If
    End Sub
    Public Sub Actualizar()

        Dim strSqlQry As String
        Dim cnConn As New SqlConnection

        cnConn.ConnectionString = CitraConnection
        strSqlQry = "Update SubServicios Set " & _
                    "Costo=@Costo " & _
                    "Where Nombre=@Nombre"


        Dim Command As New SqlCommand(strSqlQry, cnConn)

        Command.Parameters.AddWithValue("Nombre", mstrNombre)
        Command.Parameters.AddWithValue("Costo", mdblCosto)
        cnConn.Open()

        Dim RowsAffected As Integer = Command.ExecuteNonQuery

        If RowsAffected > 0 Then
            cnConn.Close()
        Else
            MsgBox("No fue posible actualizar el subservicio ", MsgBoxStyle.Exclamation, "Aviso")
            If cnConn.State = ConnectionState.Open Then
                cnConn.Close()
            End If
        End If



    End Sub
End Class
